package com.shop2cn.iapi.sqrefund.testcase;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ymttest.business.service.shenqi.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.cdfgeteway.resp.PointsVo;
import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.utill.Utils;
import com.shop2cn.iapi.sqoptrade.resp.Order;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqrefund.parameter.FullRefundReq;
import com.shop2cn.iapi.sqrefund.service.FullRefundCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.model.sqrefund.RefundBill;
import com.ymttest.database.model.sqrefund.RefundBillIndex;
import com.ymttest.database.model.sqrefund.RefundProductDetail;
import com.ymttest.database.sharding.db.ext.model.OrderDetailExtPo;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailSubCodePo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sharding.model.PaymentPo;
import com.ymttest.database.sharding.db.sharding.model.UserRefundFundDetailPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sharding.db.sqlwapper.OrderWapper;
import com.ymttest.database.sqlwapper.sqrefund.RefundBillIndexWrapper;
import com.ymttest.database.sqlwapper.sqrefund.RefundBillWrapper;
import com.ymttest.database.sqlwapper.sqrefund.RefundProductDetailWrapper;
import com.ymttest.utils.DataUtil;

/**
 * 发货 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_UserFullRefund {

	private static FullRefundReq fullRefundReq;
	private static FullRefundCall fullRefundCall;

	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	private static OrderWapper orderWapper = new OrderWapper();

	private static RefundBillWrapper refundBillWrapper = new RefundBillWrapper();
	private static RefundProductDetailWrapper refundProductDetailWrapper = new RefundProductDetailWrapper();
	private static RefundBillIndexWrapper refundBillIndexWrapper = new RefundBillIndexWrapper();

	private static long autotest_goodsOwnerId = ShenqiUserSet.autotest_goodsOwnerId;
	private static long chainStoreGoodsOwnerId = ShenqiUserSet.chainStoreGoodsOwnerId;
	
	private static long cdfMembergoodsOwnerId = ShenqiUserSet.cdfMembergoodsOwnerId;
	
	private static long virtualCoinGoodsOwnerId = ShenqiUserSet.virtualCoinGoodsOwnerId;
	private static long multiCDFCodeGoodsOwnerId = ShenqiUserSet.multiCDFCodeGoodsOwnerId;

	@BeforeClass
	public static void setUp() {

		Logger.createResultFile("买家整单退款");
	}

	@Before
	public void caseUp() {

		fullRefundReq = new FullRefundReq();
		fullRefundCall = new FullRefundCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(fullRefundCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verify(long orderId) {
		this.verify(orderId, null, false, false, false);
	}
	
	public void verify(long orderId, String rejectedRefundNo) {
		this.verify(orderId, rejectedRefundNo, false, false, false);
	}
	
	public void verify(long orderId, String rejectedRefundNo, Boolean isDistributorOrder) {
		this.verify(orderId, rejectedRefundNo, isDistributorOrder, false);
	}
	
	public void verify(long orderId, String rejectedRefundNo, Boolean isDistributorOrder, Boolean isProxyFissionOrder) {
		this.verify(orderId, rejectedRefundNo, isDistributorOrder, isProxyFissionOrder, false);
	}

	public void verify(long orderId, String rejectedRefundNo,
			Boolean isDistributorOrder, Boolean isProxyFissionOrder, Boolean isNoMoney) {

		OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);

		List<OrderDetailPo> orderDetailPoList = orderShardingWapper
				.selectOrderDetailbyOrderId(orderId);

		orderDetailPoList = orderDetailPoList
				.stream()
				.sorted(Comparator.comparing(OrderDetailPo::getCatalogId)
						.reversed()).collect(Collectors.toList());

		// 整单退款，每个商品一个退款单
		for (OrderDetailPo orderDetailPo : orderDetailPoList) {

			RefundBill expectRefundBill = new RefundBill();

			Integer refundNum = orderDetailPo.getProductCount();

			BigDecimal refundFreightAmount = RefundService
					.getMaxCanRefundFreight(orderDetailPo,
							orderDetailPo.getFreight());

			BigDecimal refundProductAmount = Utils
					.zeroIfNull(orderDetailPo.getProductPrice())
					.multiply(new BigDecimal(refundNum))
					.add(Utils.zeroIfNull(orderDetailPo.getDiscount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo.getYmtCouponAmount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo
									.getSellerCouponAmount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo.getPromotionAmount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo
									.getYmtPromotionAmount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo
									.getIntegralDeductionAmount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo
									.getRegionDiscountAmount()));

			BigDecimal refundTaxAmount = RefundService
					.getOrderDetailTaxFeeAmount(orderDetailPo, refundNum);
			
			BigDecimal refundServiceAmount = Utils
					.zeroIfNull(orderDetailPo.getServiceAmount())
					.multiply(new BigDecimal(refundNum))
					.divide(new BigDecimal(orderDetailPo.getProductCount()), 2,
							BigDecimal.ROUND_DOWN);

			// 创建expectRefundProductDetail
			RefundProductDetail expectRefundProductDetail = new RefundProductDetail();

			OrderDetailExtPo orderDetailExtPo = orderWapper
					.selectOrderDetailExtByOrderId(orderDetailPo
							.getOrderDetailId());

			expectRefundProductDetail.setOrder_id(orderId);
			expectRefundProductDetail.setCatalog_id(orderDetailPo
					.getCatalogId());
			expectRefundProductDetail.setProduct_num(orderDetailPo
					.getProductCount());
			expectRefundProductDetail.setProduct_title(orderDetailExtPo
					.getTitle());
			expectRefundProductDetail
					.setRefund_product_amount(refundProductAmount);
			expectRefundProductDetail
					.setRefund_freight_amount(refundFreightAmount);
			expectRefundProductDetail.setRefund_tax_amount(refundTaxAmount);
			expectRefundProductDetail.setRefund_num(refundNum);
			
			expectRefundProductDetail.setRefund_integral(orderDetailPo.getIntegral());
			
			expectRefundProductDetail.setRefund_service_amount(refundServiceAmount);
			
			// 如果是一品多码商品且未发货
			if (orderPo.getGoodsOwnerId() == multiCDFCodeGoodsOwnerId
					&& orderPo.getOrderStatus() != 3) {

				List<OrderDetailSubCodePo> orderDetailSubCodePoList = orderShardingWapper
						.selectOrderDetailSubCodebyOrderIdAndCatalogId(orderId,
								expectRefundProductDetail.getCatalog_id());

				Integer actualCount = orderDetailSubCodePoList.stream()
						.map(detail -> detail.getRefundProductCount())
						.reduce(Integer::sum).orElse(0);

				Logger.verifyEquals(
						actualCount,
						expectRefundProductDetail.getRefund_num(),
						String.format("验证%s子码退款数量",
								expectRefundProductDetail.getCatalog_id()));
			}
			
			String refundNo = this
					.verifyRefundProductDetailTable(expectRefundProductDetail, rejectedRefundNo);

			Long userId = orderPo.getUserId();
			Long applyUserId = 0L;
			String submitTradeResult = null;
			Integer refundStatus = 0;

			// 是否买家提交
			Boolean isUserApply = false;

			if (userId.intValue() == fullRefundReq.getUserId().intValue())
				isUserApply = true;

			if (isUserApply) {
				refundStatus = 3;
				applyUserId = userId;
			}
			
			if (isDistributorOrder) {
				
				refundStatus = 4;
				submitTradeResult = "该订单因分销佣金已结算，无法退款";
			}
			
			if (isProxyFissionOrder) {
				
				refundStatus = 4;
				submitTradeResult = "该订单因代理裂变佣金已结算，无法退款";
			}
			
//			if (isNoMoney) {
//				
//				refundStatus = 4;
//				submitTradeResult = "暂不支持的结算状态";
//			}

			expectRefundBill.setOrder_id(orderId);
			expectRefundBill.setRefund_type(3);
			expectRefundBill.setPlace_order_time(orderPo.getAddTime());
			expectRefundBill
					.setTotal_refund_product_amount(refundProductAmount);
			expectRefundBill.setTotal_refund_tax_amount(refundTaxAmount);
			expectRefundBill
					.setTotal_refund_freight_amount(refundFreightAmount);
			expectRefundBill.setOrder_status(orderPo.getOrderStatus());
			expectRefundBill.setSeller_id(orderPo.getSellerId());
			expectRefundBill.setGoods_owner_id(orderPo.getGoodsOwnerId());
			expectRefundBill.setUser_id(userId);
			expectRefundBill.setApply_user_id(applyUserId);
			expectRefundBill.setRefund_status(refundStatus);
			expectRefundBill
					.setCurrent_review_mch_id(orderPo.getGoodsOwnerId());
			expectRefundBill.setMch_review_status(2);
			expectRefundBill.setSubmit_trade_status(1);
			expectRefundBill.setRemark(fullRefundReq.getRemark());
			expectRefundBill.setRefund_no(refundNo);
			expectRefundBill.setSubmit_trade_result(submitTradeResult);
			
			expectRefundBill.setTotal_refund_integral(orderDetailPo.getIntegral());

			this.verifyRefundBillTable(orderId, expectRefundBill);
			
			RefundBillIndex expectResult = new RefundBillIndex();
			
			expectResult.setRefund_no(expectRefundBill.getRefund_no());
			expectResult.setMerchant_id(expectRefundBill.getSeller_id());
			expectResult.setOrder_status(expectRefundBill.getOrder_status());
			expectResult.setRefund_status(expectRefundBill.getRefund_status());
			expectResult.setOrder_id(orderId);
			expectResult.setRefund_type(expectRefundBill.getRefund_type());
			expectResult.setCurrent_review_mch_id(expectRefundBill.getCurrent_review_mch_id());
			expectResult.setMch_review_status(expectRefundBill.getMch_review_status());
			
			this.verifyRefundBillIndexTable(expectResult);
			
			if (refundStatus == 3) {
				
				boolean isAccountPaid = false;
				
				if (orderPo.getAccountPaidAmount().compareTo(BigDecimal.ZERO) == 1)
					isAccountPaid = true;
				else
					isAccountPaid = false;
				
				RefundService.buildUserRefundFundDetail(expectRefundBill, orderPo.getPaymentId(), isAccountPaid, refundServiceAmount);
			}
		}
	}

	private String verifyRefundBillTable(Long orderId, RefundBill expectResult) {

		String refundNo = expectResult.getRefund_no();

		Logger.comment(String.format("验证refund_bill表， orderId：%s，refundNo：%s",
				orderId, refundNo));

		RefundBill actualResult = refundBillWrapper.selectByRefundNoAndOrderId(
				orderId, refundNo);

		Logger.verifyEquals(expectResult.getRefund_type(),
				actualResult.getRefund_type(), "校验Refund_type");
		Logger.verifyEquals(refundNo, actualResult.getRefund_no(),
				"校验Refund_no");
		DataUtil.verifyDecimalEquals(
				expectResult.getTotal_refund_product_amount(),
				actualResult.getTotal_refund_product_amount(),
				"校验Total_refund_product_amount");
		DataUtil.verifyDecimalEquals(expectResult.getTotal_refund_tax_amount(),
				actualResult.getTotal_refund_tax_amount(),
				"校验Total_refund_tax_amount");
		DataUtil.verifyDecimalEquals(
				expectResult.getTotal_refund_freight_amount(),
				actualResult.getTotal_refund_freight_amount(),
				"校验Total_refund_freight_amount");
		Logger.verifyEquals(expectResult.getOrder_status(),
				actualResult.getOrder_status(), "校验Order_status");
		Logger.verifyEquals(expectResult.getSeller_id(),
				actualResult.getSeller_id(), "校验Seller_id");
		Logger.verifyEquals(expectResult.getGoods_owner_id(),
				actualResult.getGoods_owner_id(), "校验Goods_owner_id");
		Logger.verifyEquals(expectResult.getUser_id(),
				actualResult.getUser_id(), "校验User_id");
		Logger.verifyEquals(expectResult.getApply_user_id(),
				actualResult.getApply_user_id(), "校验Apply_user_id");
		Logger.verifyEquals(expectResult.getRefund_status(),
				actualResult.getRefund_status(), "校验Refund_status");
		Logger.verifyEquals(expectResult.getCurrent_review_mch_id(),
				actualResult.getCurrent_review_mch_id(),
				"校验Current_review_mch_id");
		Logger.verifyEquals(expectResult.getMch_review_status(),
				actualResult.getMch_review_status(), "校验Mch_review_status");
		Logger.verifyEquals(expectResult.getSubmit_trade_result(),
				actualResult.getSubmit_trade_result(), "校验Submit_trade_result");
		Logger.verifyEquals(expectResult.getRemark(), actualResult.getRemark(),
				"校验Remark");
		
		Logger.verifyEquals(expectResult.getTotal_refund_integral(), actualResult.getTotal_refund_integral(),
				"校验Total_refund_integral");
		
		Logger.verifyEquals(fullRefundReq.getLiabilityType(),
				actualResult.getLiability_type(), "校验Liability_type");
		Logger.verifyEquals(fullRefundReq.getRefundExtType(),
				actualResult.getRefund_ext_type(), "校验Refund_ext_type");
		Logger.verifyEquals(fullRefundReq.getRefundReasonCode(),
				actualResult.getRefund_reason_code(), "校验Refund_reason_code");
		Logger.verifyEquals(fullRefundReq.getHasRefundGoods(),
				actualResult.getHas_refund_goods(), "校验Has_refund_goods");
		
		return actualResult.getRefund_no();
	}

	private String verifyRefundProductDetailTable(
			RefundProductDetail expectResult, String rejectedRefundNo) {

		Long orderId = expectResult.getOrder_id();
		String catalogId = expectResult.getCatalog_id();

		Logger.comment(String.format(
				"验证refund_product_detail表， orderId：%s，catalogId：%s", orderId,
				catalogId));

		// 找到退款成功的退款详情单
		RefundProductDetail actualResult = refundProductDetailWrapper
				.selectByOrderIdAndCatalogId(orderId, catalogId)
				.stream()
				.filter(refundProductDetail -> !refundProductDetail
						.getRefund_no().equalsIgnoreCase(rejectedRefundNo))
				.collect(Collectors.toList()).get(0);

		DataUtil.verifyDecimalEquals(expectResult.getRefund_product_amount(),
				actualResult.getRefund_product_amount(),
				"校验Refund_product_amount");
		DataUtil.verifyDecimalEquals(expectResult.getRefund_tax_amount(),
				actualResult.getRefund_tax_amount(), "校验Refund_tax_amount");
		DataUtil.verifyDecimalEquals(expectResult.getRefund_freight_amount(),
				actualResult.getRefund_freight_amount(),
				"校验Refund_freight_amount");
		Logger.verifyEquals(expectResult.getRefund_num(),
				actualResult.getRefund_num(), "校验Refund_num");
		Logger.verifyEquals(expectResult.getProduct_num(),
				actualResult.getProduct_num(), "校验Product_num");
		Logger.verifyEquals(expectResult.getProduct_title(),
				actualResult.getProduct_title(), "校验Product_title");
		
		Logger.verifyEquals(expectResult.getRefund_integral(),
				actualResult.getRefund_integral(), "校验Refund_integral");

		return actualResult.getRefund_no();
	}
	
	private void verifyRefundBillIndexTable(RefundBillIndex expectResult) {
		
		Long merchantId = expectResult.getMerchant_id();
		
		Logger.comment(String.format(
				"验证refund_bill_index表， refundNo：%s，merchantId：%s", expectResult.getRefund_no(),
				merchantId));
		
		RefundBillIndex actualResult = refundBillIndexWrapper
				.selectByRefundNoAndMerchantId(expectResult.getRefund_no(),
						merchantId);

		Logger.verifyEquals(expectResult.getOrder_status(),
				actualResult.getOrder_status(),
				"校验Order_status");
		Logger.verifyEquals(expectResult.getRefund_status(),
				actualResult.getRefund_status(), "校验Refund_status");
		Logger.verifyEquals(expectResult.getOrder_id(),
				actualResult.getOrder_id(),
				"校验Order_id");
		Logger.verifyEquals(expectResult.getRefund_type(),
				actualResult.getRefund_type(), "校验Refund_type");
		Logger.verifyEquals(expectResult.getCurrent_review_mch_id(),
				actualResult.getCurrent_review_mch_id(), "校验Current_review_mch_id");
		Logger.verifyEquals(expectResult.getMch_review_status(),
				actualResult.getMch_review_status(), "校验Mch_review_status");
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_001() {
		Logger.start(false, "直销订单，未接单，买家申请整单退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(1);
			fullRefundReq.setRefundReasonCode(2);
			fullRefundReq.setHasRefundGoods(true);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.HOUR, -24);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			SQPayGateWayCallService.updatePayerEmailByTradingID(String
					.valueOf(paymentId));

			Thread.sleep(5000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_002() {
		Logger.start(false, "直销订单，已接单，买家申请整单退款失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId(userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(3);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(4);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			Logger.verifyEquals("当前订单状态，您不能发起整单退款", fullRefundCall.getMsg(),
					"验证resultMessage");
			Logger.verifyEquals("3", fullRefundCall.getCode(), "验证resultCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_003() {
		Logger.start(false, "代理订单，用户申请整单退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);
			
			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId(userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(3);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(4);
			fullRefundReq.setHasRefundGoods(null);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();
			
//			for (RefundBill refundBill : refundBillWrapper
//					.selectByOrderId(orderId)) {
//
//				String refundNo = refundBill.getRefund_no();
//
//				// 审核地址
//				RefundService.auditRefundGoods(autotest_goodsOwnerId,
//						autotest_goodsOwnerId,
//						CurrentMchRefundStatusEnum.LEVEL_ONE.getCode(),
//						refundNo, true);
//			}
			
			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.HOUR, -24);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(5000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_004() {
		Logger.start(false, "直销订单，已经整单退款成功的订单，再次整单退款，退款失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(null);
			fullRefundReq.setRefundExtType(null);
			fullRefundReq.setRefundReasonCode(-1);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.HOUR, -24);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			// 再次申请整单退款
			fullRefundCall.callService();

			Logger.verifyEquals("当前状态不能退货退款", fullRefundCall.getMsg(),
					"验证resultMessage");
			Logger.verifyEquals("3", fullRefundCall.getCode(), "验证resultCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_005() {
		Logger.start(false, "直销订单，订单正在整单退款，再次整单退款，退款失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) autotest_goodsOwnerId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(-1);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 再次申请整单退款
			fullRefundCall.callService();

			Logger.verifyEquals("已有退款，无法整单退款", fullRefundCall.getMsg(),
					"验证resultMessage");
			Logger.verifyEquals("3", fullRefundCall.getCode(), "验证resultCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_006() {
		Logger.start(false, "直销订单，盛付通支付，买家申请整单退款，退款成功");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer", "6010");

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6010);

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();
			
			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.HOUR, -24);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(5000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_007() {
		Logger.start(false, "直销订单，超过90天的订单，买家申请整单退款，退款失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			// 修改支付时间为90天前
			OrderPo record = new OrderPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DAY_OF_MONTH, -101);

			record.setOrderId(orderId);
			record.setPaidTime(calendar.getTime());

			orderShardingWapper.orderMapper.updateByPrimaryKeySelective(record);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			Logger.verifyEquals("订单已支付超过90天无法发起退款", fullRefundCall.getMsg(),
					"验证resultMessage");
			Logger.verifyEquals("3", fullRefundCall.getCode(), "验证resultCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_008() {
		Logger.start(false, "直销订单，包含虚拟库存，买家申请整单退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId, 2);

			Order order = placeOrderResp.getOrderList().get(0);
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.HOUR, -24);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);
			
			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_009() {
		Logger.start(false, "直销订单，人民币标价商品，商户不允许收取人民币，未发货，买家申请整单退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) 500956988, 0, false, false);
			String currency = productBean.getCurrency();

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, null, 1, 500956988L, currency);

			Order order = placeOrderResp.getOrderList().get(0);
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer", "6000");

			// 根据人民币离岸汇率计算
			BigDecimal exchangeRmbRate = DGSQBaseService.getCurrencyConfig(
					"USD").getExchangeRmbRate();

			amount = amount.multiply(
					BigDecimal.ONE.divide(exchangeRmbRate, 7,
							BigDecimal.ROUND_UP)).setScale(2,
					BigDecimal.ROUND_UP);

			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					currency, currency);

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.HOUR, -24);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);
			
			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_010() {
		Logger.start(false, "直销订单，带有服务费率，买家申请整单退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithServiceRate(productBean);

			Order order = placeOrderResp.getOrderList().get(0);
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.HOUR, -24);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);
			
			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_011() {
		Logger.start(false, "代理订单，商户余额不足，买家申请整单退款，可以申请退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderNegativeProfit(productBean, BigDecimal.ZERO,
							autotest_goodsOwnerId, productBean.getCurrency(),
							null, null);

			Order order = placeOrderResp.getOrderList().get(0);
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = orderShardingWapper
					.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(thirdTradingId, payment,
					null, 1);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.HOUR, -24);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);
			
			Thread.sleep(3000);
			
			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);
			
			this.verify(orderId, null, false, false, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	@Ignore
	public void Tc_UserFullRefund_012() {
		Logger.start(false, "直销订单，连锁店模式，未发货，买家申请整单退款，退款成功");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) chainStoreGoodsOwnerId, 0, false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeChainStoreOrder(productBean, chainStoreGoodsOwnerId,
							productBean.getCurrency());

			Order order = placeOrderResp.getOrderList().get(0);
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "501476063", "6401");

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6401);

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);
			
			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_013() {
		Logger.start(false, "代理订单，已线下付款的订单，买家申请整单退款，退款失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			String currency = productBean.getCurrency();
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, BigDecimal.ZERO,
							autotest_goodsOwnerId, currency, true);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();

			// 支付线下支付订单
			SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer", "200");
			SqTradingCallService.confirmOfflinePay(orderId,
					autotest_goodsOwnerId);

			Thread.sleep(5000);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			Logger.verifyEquals("线下支付订单，不能退货退款", fullRefundCall.getMsg(),
					"验证resultMessage");
			Logger.verifyEquals("3", fullRefundCall.getCode(), "验证resultCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_014() {
		Logger.start(false, "代理订单，使用积分，买家申请整单退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);
			
			Long shopId = productBean.getSellerId();
			
			GodPointsService.saveCreditPolicyReq((long)shopId);
			Thread.sleep(500);
			
			GodPointsService.sendCredit((long)shopId, 20238699L);
			Thread.sleep(500);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithIntegral(productBean, autotest_goodsOwnerId, 20L);

			Order order = placeOrderResp.getOrderList().get(0);
			
			Long userId = placeOrderResp.getUserId();
			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();
			
			// 退款提交到网关，第一次退积分，第二次退金额
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			for (int i = 0; i < 3; i++) {

				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.MINUTE, -6 * (i + 10));

				record.setPaymentId(paymentId);
				record.setUpdateTime(calendar.getTime());

				orderShardingWapper.userRefundFundDetailMapper
						.updateByPrimaryKeySelective(record);

				RefundService.supplementRefund(orderId);

				Thread.sleep(5000);
			}
			
			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_015() {
		Logger.start(false, "直销订单，主商品带换购商品，未发货，货头申请整单退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithExchangeBuy(productBean, productBean.getGoodsOwnerId());

			Order order = placeOrderResp.getOrderList().get(0);
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(5000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_016() {
		Logger.start(false, "使用中免大会员积分，买家申请整单退款");
		try {
			
			CDFMemberService.getCDFMemberUserPoints(cdfMembergoodsOwnerId,
					26080783L);

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createMultiCodeProxychain((int) cdfMembergoodsOwnerId,
							false);
			
			long integral = 10L;

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithCDFMember(productBean, cdfMembergoodsOwnerId, integral);

			Order order = placeOrderResp.getOrderList().get(0);
			
			Long userId = placeOrderResp.getUserId();
			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);
			
			// 获取退款成功前中免大会员消耗的积分
			PointsVo pointsVo = CDFMemberService.getCDFMemberUserPoints(
					cdfMembergoodsOwnerId, 26080783L);

			Long balance = pointsVo.getTotalConsume();

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关，第一次退积分，第二次退金额
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();
			
			for (int i = 0; i < 2; i++) {
				
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.MINUTE, -6 * (i + 10));

				record.setPaymentId(paymentId);
				record.setUpdateTime(calendar.getTime());

				orderShardingWapper.userRefundFundDetailMapper
						.updateByPrimaryKeySelective(record);

				RefundService.supplementRefund(orderId);
				
				Thread.sleep(5000);
			}

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);

			this.verify(orderId);

			// 获取退款成功后中免大会员消耗的积分
			PointsVo afterRefundPointsVo = CDFMemberService.getCDFMemberUserPoints(
					cdfMembergoodsOwnerId, 26080783L);

			Long afterRefundTotalConsume = afterRefundPointsVo.getTotalConsume();
			
			Logger.verifyEquals(integral, balance - afterRefundTotalConsume,
					"验证成功退款后返还的抵扣积分");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_017() {
		Logger.start(false, "使用中免大会员积分，组合商品，买家申请整单退款");
		try {
			
			long integral = 358L;

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeCDFMemberComposeProductOrder(cdfMembergoodsOwnerId, integral);

			Order order = placeOrderResp.getOrderList().get(0);
			
			Long userId = placeOrderResp.getUserId();
			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 获取退款成功前中免大会员消耗的积分
			PointsVo pointsVo = CDFMemberService.getCDFMemberUserPoints(
					cdfMembergoodsOwnerId, 26080783L);

			Long balance = pointsVo.getTotalConsume();

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关，第一次退积分，第二次退金额
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();
			
			for (int i = 0; i < 3; i++) {
				
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.MINUTE, -6 * (i + 10));

				record.setPaymentId(paymentId);
				record.setUpdateTime(calendar.getTime());

				orderShardingWapper.userRefundFundDetailMapper
						.updateByPrimaryKeySelective(record);

				RefundService.supplementRefund(orderId);
				
				Thread.sleep(5000);
			}

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);

			this.verify(orderId);

			// 获取退款成功后中免大会员消耗的积分
			PointsVo afterRefundPointsVo = CDFMemberService.getCDFMemberUserPoints(
					cdfMembergoodsOwnerId, 26080783L);

			Long afterRefundTotalConsume = afterRefundPointsVo.getTotalConsume();
			
			Logger.verifyEquals(integral, balance - afterRefundTotalConsume,
					"验证成功退款后返还的抵扣积分");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	@Ignore
	// 特殊用例
	public void Tc_UserFullRefund_018() {
		Logger.start(false, "使用中免大会员积分，已发货，第二个订单使用所有积分。第一个订单买家申请整单退款");
		try {
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) cdfMembergoodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithCDFMember(productBean, cdfMembergoodsOwnerId, 100L);

			Order order = placeOrderResp.getOrderList().get(0);
			
			Long userId = placeOrderResp.getUserId();
			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);
			
			SqTradingCallService.lockCDFMemberIntegral(cdfMembergoodsOwnerId);
			
			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关，第一次退积分，第二次退金额
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();
			
			for (int i = 0; i < 2; i++) {
				
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.MINUTE, -6 * (i + 10));

				record.setPaymentId(paymentId);
				record.setUpdateTime(calendar.getTime());

				orderShardingWapper.userRefundFundDetailMapper
						.updateByPrimaryKeySelective(record);

				RefundService.supplementRefund(orderId);
				
				Thread.sleep(5000);
			}

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);

			this.verify(orderId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_019() {
		Logger.start(false, "使用中免大会员积分0分，已发货，货头申请整单退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) cdfMembergoodsOwnerId, 0, false,
							false);
			
			long integral = 0L;

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithCDFMember(productBean, cdfMembergoodsOwnerId, 0L);

			Order order = placeOrderResp.getOrderList().get(0);
			
			Long userId = placeOrderResp.getUserId();
			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = orderShardingWapper
					.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(thirdTradingId, payment,
					null, 1);
			
			// 获取退款成功前中免大会员消耗的积分
			PointsVo pointsVo = CDFMemberService.getCDFMemberUserPoints(
					cdfMembergoodsOwnerId, 26080783L);

			Long balance = pointsVo.getTotalConsume();

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关，第一次退积分，第二次退金额
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();
			
			for (int i = 0; i < 2; i++) {
				
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.MINUTE, -6 * (i + 10));

				record.setPaymentId(paymentId);
				record.setUpdateTime(calendar.getTime());

				orderShardingWapper.userRefundFundDetailMapper
						.updateByPrimaryKeySelective(record);

				RefundService.supplementRefund(orderId);
				
				Thread.sleep(5000);
			}

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);

			this.verify(orderId);

			// 获取退款成功后中免大会员消耗的积分
			PointsVo afterRefundPointsVo = CDFMemberService.getCDFMemberUserPoints(
					cdfMembergoodsOwnerId, 26080783L);

			Long afterRefundTotalConsume = afterRefundPointsVo.getTotalConsume();
			
			Logger.verifyEquals(integral, balance - afterRefundTotalConsume,
					"验证成功退款后返还的抵扣积分");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_020() {
		Logger.start(false, "使用中免大会员积分，包含所有优惠，已发货，货头申请整单退款");
		try {
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) cdfMembergoodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeAllPromotionOrderWithCDFMember(productBean, cdfMembergoodsOwnerId, 10L, false);

			Order order = placeOrderResp.getOrderList().get(0);
			
			Long userId = placeOrderResp.getUserId();
			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);
			
			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关，第一次退积分，第二次退金额
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();
			
			for (int i = 0; i < 4; i++) {
				
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.MINUTE, -6 * (i + 10));

				record.setPaymentId(paymentId);
				record.setUpdateTime(calendar.getTime());

				orderShardingWapper.userRefundFundDetailMapper
						.updateByPrimaryKeySelective(record);

				RefundService.supplementRefund(orderId);
				
				Thread.sleep(3000);
			}

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(5000);

			this.verify(orderId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_021() {
		Logger.start(false, "使用中免大会员积分，其中一个商品金额退款金额为0，买家申请整单退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) cdfMembergoodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderOneItemZeroWithCDFMember(productBean, cdfMembergoodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);
			
			Long userId = placeOrderResp.getUserId();
			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关，第一次退积分，第二次退金额
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();
			
			for (int i = 0; i < 4; i++) {
				
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.MINUTE, -6 * (i + 10));

				record.setPaymentId(paymentId);
				record.setUpdateTime(calendar.getTime());

				orderShardingWapper.userRefundFundDetailMapper
						.updateByPrimaryKeySelective(record);

				RefundService.supplementRefund(orderId);
				
				Thread.sleep(3000);
			}

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(5000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_022() {
		Logger.start(false, "直销订单，使用积分，使用代币支付，未发货，买家申请整单退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createIndependenceProxychain((int)virtualCoinGoodsOwnerId, 0, false, false);
			
			Long shopId = productBean.getSellerId();
			
			GodPointsService.saveCreditPolicyReq((long)shopId);
			Thread.sleep(500);
			
			GodPointsService.sendCredit((long)shopId, shopId);
			Thread.sleep(500);
			
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndSalemanAndGoodsOwnerIdAndIntegral(
							productBean, productBean.getSellerId(), null,
							virtualCoinGoodsOwnerId, 10L);

			Order order = placeOrderResp.getOrderList().get(0);

			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();
			Long tradingId = placeOrderResp.getTradingId();
			
			SqTradingMqService.updateLiveOrder(false, orderId);
			
			// 充值代币
			SqPretradeService.virtualCoinCharge(virtualCoinGoodsOwnerId,
					productBean.getSellerId(), useAccountAmount);

			// 支付订单
			SqTradingCallService.payOrderWithVirtualCoin(userId, tradingId, useAccountAmount);

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId(userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡"); 
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(2);
			fullRefundReq.setRefundReasonCode(3);
			fullRefundReq.setHasRefundGoods(false);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();
			
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();
			
			for (int i = 0; i < 4; i++) {
				
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.MINUTE, -6);

				record.setPaymentId(paymentId);
				record.setUpdateTime(calendar.getTime());

				orderShardingWapper.userRefundFundDetailMapper
						.updateByPrimaryKeySelective(record);

				RefundService.supplementRefund(orderId);

				Thread.sleep(3000);
			}

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_023() {
		Logger.start(false, "一品多码商品订单，买家申请整单退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createMultiCodeProxychain((int) multiCDFCodeGoodsOwnerId, false);
			
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeMultiCDFCodeOrder(productBean, multiCDFCodeGoodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);
			
			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(1);
			fullRefundReq.setRefundReasonCode(2);
			fullRefundReq.setHasRefundGoods(true);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.HOUR, -24);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(5000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserFullRefund_024() {
		Logger.start(false, "直销代付订单，买家申请整单退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), true);

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			fullRefundReq.setOrderId(orderId);
			fullRefundReq.setUserId((long) userId);
			fullRefundReq.setCsId(1234L);
			fullRefundReq.setRemark("remar阿斯达卡");
			fullRefundReq.setLiabilityType(1);
			fullRefundReq.setRefundExtType(1);
			fullRefundReq.setRefundReasonCode(2);
			fullRefundReq.setHasRefundGoods(true);

			fullRefundCall.setData(fullRefundReq);
			fullRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.HOUR, -24);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(5000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) {

		Ts_UserFullRefund test = new Ts_UserFullRefund();

		Long orderId = 1450710783L;

		fullRefundReq = new FullRefundReq();

		fullRefundReq.setOrderId(orderId);
		fullRefundReq.setUserId((long) cdfMembergoodsOwnerId);
		fullRefundReq.setCsId(1234L);
		fullRefundReq.setRemark("remar阿斯达卡");

		test.verify(orderId);
	}
}